<% layout('/layouts/default.html', {title: '采购单管理', libs: ['validate','dataGrid']}){ %>
<div class="main-content">
	<div class="box box-main">
		<div class="box-header with-border">
			<div class="box-title">
				<i class="fa icon-note"></i> ${text(stPurchaseBill.isNewRecord ? '新增采购单' : '编辑采购单')}
			</div>
			<div class="box-tools pull-right">
				<a href="#" class="btn btn-default" id="btn-approve" ><i class="fa fa-filter"></i> ${text('审核')}
				</a>
				<a href="#" class="btn btn-default" id="btn-revocation" ><i class="fa fa-filter"></i> ${text('弃审')}
				</a>
				<a href="#" class="btn btn-default" id="btn-save" ><i class="fa fa-filter"></i> ${text('保存')}
				</a>
				<button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i></button>
			</div>
			<script>
				var aStatus=${stPurchaseBill.approveState!"null"};
				approveBtnStatus(aStatus);
				function approveBtnStatus(status) {
					var $save=$("#btn-save");
					var $approve=$("#btn-approve");
					var $revocation=$("#btn-revocation");
					if (null == status ) {
						$save.show();
						$approve.hide();
						$revocation.hide();
					}
					if (2 == status) {
						$save.show();
						$approve.show();
						$revocation.hide();
					}
					if (4 == status) {
						$save.hide();
						$approve.hide();
						$revocation.show();
					}
				}
				function refresh(id) {
					location.replace("${ctx}/st/stPurchaseBill/form?id="+id);
					// location.reload();
				}
				$("#btn-save").click(function () {
					$("#approveState").val(2);
					$("form").submit();
				});
				$("#btn-approve").click(function () {
					if(null == aStatus){
						js.showMessage("还没保存呢");
						return;
					}
					$("#approveState").val(4);
					$("form").submit();
				});
				$("#btn-revocation").click(function () {
					$("#approveState").val(2);
					$("form").submit();
				});
			</script>
		</div>
		<#form:form id="inputForm" model="${stPurchaseBill}" action="${ctx}/st/stPurchaseBill/save" method="post" class="form-horizontal">
			<div class="box-body">
				<div class="form-unit">${text('基本信息')}</div>
				<#form:hidden path="id"/>
				<div class="row">
<!--					<div class="col-xs-6">-->
<!--						<div class="form-group">-->
<!--							<label class="control-label col-sm-4" title="">-->
<!--								<span class="required hide">*</span> ${text('单据号')}：<i class="fa icon-question hide"></i></label>-->
<!--							<div class="col-sm-8">-->
<!--								<#form:input path="billNo" maxlength="20" class="form-control"/>-->
<!--							</div>-->
<!--						</div>-->
<!--					</div>-->
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('单据日期')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="billDate" readonly="true" maxlength="20" class="form-control laydate"
									dataFormat="datetime" data-type="datetime" data-format="yyyy-MM-dd HH:mm"/>
							</div>
						</div>
					</div>
<!--					<div class="col-xs-6">-->
<!--						<div class="form-group">-->
<!--							<label class="control-label col-sm-4" title="">-->
<!--								<span class="required hide">*</span> ${text('采购员编号')}：<i class="fa icon-question hide"></i></label>-->
<!--							<div class="col-sm-8">-->
<!--								<#form:input path="buyerCode" maxlength="20" class="form-control"/>-->
<!--							</div>-->
<!--						</div>-->
<!--					</div>-->
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('采购员')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:treeselect id="buyerName" title="用户选择" path="buyerCode" labelPath="buyerName"
								url="${ctx}/sys/office/treeData?isLoadUser=true" class="" allowClear="true"/>
							</div>
						</div>
					</div>
<!--					<div class="col-xs-6">-->
<!--						<div class="form-group">-->
<!--							<label class="control-label col-sm-4" title="">-->
<!--								<span class="required hide">*</span> ${text('部门编码')}：<i class="fa icon-question hide"></i></label>-->
<!--							<div class="col-sm-8">-->
<!--								<#form:input path="deptCode" maxlength="20" class="form-control"/>-->
<!--							</div>-->
<!--						</div>-->
<!--					</div>-->
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('部门')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:treeselect id="deptName" title="部门"
									path="deptCode" labelPath="deptName"
									url="${ctx}/sys/office/treeData" class="" allowClear="true"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required">*</span> ${text('供应商')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:select path="supplierId" items="${supplierList}" itemLabel="supplierFullName" blankOption="true" itemValue="id" class="form-control required" />
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('退/购货')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:select path="inOrOut" dictType="st_purchase_in_or_out" defaultValue="1" class="form-control digits" />
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('币别')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:select path="moneyType" dictType="st_money_type" blankOption="true" class="form-control digits" />
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('合同编号')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="contractNo" maxlength="20" class="form-control"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('采购类别')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:select path="buyType" dictType="st_buy_type" blankOption="true" class="form-control digits" />
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('付款条件')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="payWhere" maxlength="20" class="form-control"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('交货方式')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:select path="deliveryWay" dictType="st_delivery_way" blankOption="true" class="form-control digits" />
							</div>
						</div>
					</div>
					<#form:hidden path="approveState" />
<!--					<div class="col-xs-6">-->
<!--						<div class="form-group">-->
<!--							<label class="control-label col-sm-4" title="">-->
<!--								<span class="required hide">*</span> ${text('审批状态')}：<i class="fa icon-question hide"></i></label>-->
<!--							<div class="col-sm-8">-->
<!--								<#form:select path="approveState" dictType="st_approve_state" blankOption="true" class="form-control digits" />-->
<!--							</div>-->
<!--						</div>-->
<!--					</div>-->
<!--					<div class="col-xs-6">-->
<!--						<div class="form-group">-->
<!--							<label class="control-label col-sm-4" title="">-->
<!--								<span class="required hide">*</span> ${text('审批人编码')}：<i class="fa icon-question hide"></i></label>-->
<!--							<div class="col-sm-8">-->
<!--								<#form:input path="approveManCode" maxlength="20" class="form-control"/>-->
<!--							</div>-->
<!--						</div>-->
<!--					</div>-->
<!--					<div class="col-xs-6">-->
<!--						<div class="form-group">-->
<!--							<label class="control-label col-sm-4" title="">-->
<!--								<span class="required hide">*</span> ${text('审批人')}：<i class="fa icon-question hide"></i></label>-->
<!--							<div class="col-sm-8">-->
<!--								<#form:input path="approveManName" maxlength="5" class="form-control"/>-->
<!--							</div>-->
<!--						</div>-->
<!--					</div>-->
				</div>
				<div class="row">
					<div class="col-xs-12">
						<div class="form-group">
							<label class="control-label col-sm-2" title="">
								<span class="required hide">*</span> ${text('备注信息')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-10">
								<#form:textarea path="remarks" rows="4" maxlength="500" class="form-control"/>
							</div>
						</div>
					</div>
				</div>
				<h4 class="form-unit">${text('采购单详情')}</h4>
				<div class="ml10 mr10">
					<table id="stPurchaseDetailDataGrid"></table>
					<% if (hasPermi('st:stPurchaseBill:edit')){ %>
						<a href="#" id="stPurchaseDetailDataGridAddRowBtn" class="btn btn-primary btn-sm mt10 mb10"><i class="fa fa-plus"></i> ${text('增行')}</a>
					<% } %>
				</div>
			</div>
		<script src="https://cdn.bootcdn.net/ajax/libs/bignumber.js/9.0.1/bignumber.js"></script>
			<div class="box-footer">
				<div class="row">
					<div class="col-sm-offset-2 col-sm-10">
						<% if (hasPermi('st:stPurchaseBill:edit')){ %>
<!--							<button type="submit" class="btn btn-sm btn-primary" id="btnSubmit"><i class="fa fa-check"></i> ${text('保 存')}</button>&nbsp;-->
						<% } %>
						<button type="button" class="btn btn-sm btn-default" id="btnCancel" onclick="js.closeCurrentTabPage()"><i class="fa fa-reply-all"></i> ${text('关 闭')}</button>
					</div>
				</div>
			</div>
		</#form:form>
	</div>
</div>
<% } %>
<script>
//初始化采购单详情DataGrid对象
$("#stPurchaseDetailDataGrid").dataGrid({

	data: ${toJson(stPurchaseBill.stPurchaseDetailList)},
	datatype: "local", // 设置本地数据
	autoGridHeight: function(){return 'auto'}, // 设置自动高度
	
	// 设置数据表格列
	columnModel: [
		{header:'状态', name:'status', editable:true, hidden:true},
		{header:'主键', name:'id', editable:true, hidden:true},
		{header:'${text("物料名称")}', name:'materialName', width:150,
			editable:true, edittype:'select', editoptions:{'class':'form-control',
				items: $.merge([{materialName:'&nbsp;',id:''}], ${materialList}),
				itemLabel: 'materialName', itemValue: 'id', dataInit: function(element){
					$(element).select2().on("change",function(e){
						$(this).resetValid()

					});
					$(element).on("select2:select",function(e){
						var data = e.params.data;
						if('' ==data.id) return;
						var materialList=${materialList};
						var chooseItem=null;
						for(var index in materialList){
							var item =materialList[index];
							if(materialList[index].id== data.id){
								chooseItem=item;
								break;
							}
						}
						$.ajax({
							url:"${ctx}/st/stMaterialRecord/id",
							dataType:'json',
							type:'POST',
							data:{
								//请求参数
								id:data.id
							},
							success:function(result){
								var chooseItem = result;
								console.info(chooseItem);
								//数量
								var td_next_1 = $(element).closest('td').next();
								td_next_1.find('.form-control').val('0');
								//单位
								var td_next_2 = td_next_1.next();
								td_next_2.find('.form-control').val(chooseItem.unitSetting1.name);
								//单价
								var td_next_single_price=td_next_2.next();
								td_next_single_price.find('.form-control').val(chooseItem.singlePrice);
								//金额
								var td_money=td_next_single_price.next();
								// td_money.find('.form-control').val('40');
								var td_rate = td_money.next();
								td_rate.find('.form-control').val('1341929286495764480').trigger('change');//todo 这里不能写死
								//交货日期
								// var td_delivery_date = td_rate.next();
								// td_delivery_date.find('.form-control').val("")
							}
						});

					});
				}
			}
		},
		{header:'${text("数量")}', name:'number', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'8', 'class':'form-control digits',
				dataInit:function(element){

				$(element).bind("input propertychange",function (){
					var number = this.value;
					var $singlePrice = $(this).closest("td").next().next();
					var single = $singlePrice.find('.form-control').val();
					var $money = $singlePrice.next();
					var n1 = new BigNumber(number);
					var n2 = new BigNumber(single);
					$money.find('.form-control').val(n1.times(n2).toNumber())
				});
				}
			}
		},
		{header:'${text("单位")}', name:'unit', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'36', 'class':'form-control',readonly: "true",dataInit:function(element){
					// $(element).attr("readonly", true);
				}
		}
		},
		{header:'${text("单价")}', name:'singlePrice', width:150, editable:true, edittype:'text', editoptions:{'class':'form-control',
				dataInit:function(element){

					$(element).bind("input propertychange",function (){
						var single = this.value;
						var $singlePrice = $(this).closest("td").prev().prev();
						var number = $singlePrice.find('.form-control').val();
						var $money = $(this).closest("td").next();
						var n1 = new BigNumber(number);
						var n2 = new BigNumber(single);
						$money.find('.form-control').val(n1.times(n2).toNumber())
					});
				}
			}},
		{header:'${text("金额")}', name:'money', width:150, editable:true, edittype:'text', editoptions:{'class':'form-control',readonly: true}},
		{header:'${text("税率")}', name:'taxRate', width:150,
			editable:true, edittype:'select', editoptions: {
				'class': 'form-control',
				items: $.merge([{name: '&nbsp;', id: ''}], ${rateList}),
				itemLabel: 'name', itemValue: 'id', dataInit: function (element) {
					$(element).select2().on("change", function (e) {
						$(this).resetValid()
					});
				}
			}
		},
		{header:'${text("交货日期")}', name:'deliveryDate', width:150, 
			formatter:'date', formatoptions:{srcformat:'Y-m-d H:i:s',newformat:'Y-m-d H:i:s'},
			editable:true, edittype:'text', editoptions:{'class':'form-control laydate', 'readonly':'true',
				dataInit: function(element){
					laydate.render({elem:element, type:'datetime', format:'yyyy-MM-dd HH:mm'});
				}
			}
		},
		{header:'${text("现存数量")}', name:'numNow', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'8', 'class':'form-control digits'}},
		{header:'${text("备注")}', name:'remarks', width:150, editable:true, edittype:'textarea', editoptions:{'maxlength':'500', 'class':'form-control', 'rows':'1'}},
		{header:'${text("父id")}', name:'parentId.id', editable:true, hidden:true},
		{header:'${text("操作")}', name:'actions', width:80, sortable:false, fixed:true, formatter: function(val, obj, row, act){
			var actions = [];
			if (val == 'new'){
				actions.push('<a href="#" onclick="js.confirm(\'${text("你确认要删除这条数据吗？")}\', function(){$(\'#stPurchaseDetailDataGrid\').dataGrid(\'delRowData\',\''+obj.rowId+'\')});return false;"><i class="fa fa-trash-o"></i></a>&nbsp;');
			}else{
				actions.push('<a href="#" onclick="js.confirm(\'${text("你确认要删除这条数据吗？")}\', function(){$(\'#stPurchaseDetailDataGrid\').dataGrid(\'setRowData\',\''+obj.rowId+'\',null,{display:\'none\'});$(\'#'+obj.rowId+'_status\').val(\''+Global.STATUS_DELETE+'\');});return false;"><i class="fa fa-trash-o"></i></a>&nbsp;');
			}
			return actions.join('');
		}, editoptions: {defaultValue: 'new'}}
	],
	
	// 编辑表格参数
	editGrid: true,				// 是否是编辑表格
	editGridInitRowNum: 1,		// 编辑表格的初始化新增行数
	editGridAddRowBtn: $('#stPurchaseDetailDataGridAddRowBtn'),	// 子表增行按钮
	editGridAddRowInitData: {id: '', status: Global.STATUS_NORMAL},	// 新增行的时候初始化的数据
	
	// 编辑表格的提交数据参数
	editGridInputFormListName: 'stPurchaseDetailList', // 提交的数据列表名
	editGridInputFormListAttrs: 'status,id,materialName,number,unit,singlePrice,money,taxRate,deliveryDate,numNow,remarks,parentId.id,', // 提交数据列表的属性字段
	
	// 加载成功后执行事件
	ajaxSuccess: function(data){
		
	}
});
</script>
<script>
$("#inputForm").validate({
	submitHandler: function(form){
		js.ajaxSubmitForm($(form), function(data){
			var id = data.data.id;
			// $("#id").val(id);
			refresh(id);
			// aStatus=data.data.approveState;
			// approveBtnStatus(data.data.approveState);
			js.showMessage(data.message);
			// if(data.result == Global.TRUE){
			// 	js.closeCurrentTabPage(function(contentWindow){
			// 		contentWindow.page();
			// 	});
			// }
		}, "json");
    }
});
</script>